package com.dt.dubai.core.qrcode.barCodeUtil;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.oned.ITFWriter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;


@RestController
@RequestMapping("/ZxingEncoding")
public class ZxingEncoding {


    /**
     * 条形码编码 ITF
     *
     * @param contents
     * @param width
     * @param height
     * @param imgPath
     */
    @GetMapping("/encode")
    @ResponseBody
    public void encode(String contents, int width, int height, String imgPath) {
        int codeWidth =  3 + // start guard
                (7 * 6) + // left bars
                5 + // middle guard
                (7 * 6) + // right bars
                3; // end guard
        codeWidth = Math.max(codeWidth, width);
        try {
//            BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,BarcodeFormat.ITF, codeWidth, height, null);
//            MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath));
            BitMatrix bitMatrix = new ITFWriter().encode(contents,BarcodeFormat.ITF, width, height);
            MatrixToImageWriter.writeToFile(bitMatrix, "png", new File(imgPath));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 条形码解码
     *
     * @param imgPath
     * @return String
     */
    @GetMapping("/decode")
    @ResponseBody
    public String decode(String imgPath) {
        BufferedImage image = null;
        Result result = null;
        try {
            image = ImageIO.read(new File(imgPath));
            if (image == null) {
                System.out.println("the decode image may be not exit.");
            }
            LuminanceSource source = new BufferedImageLuminanceSource(image);
            BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
            result = new MultiFormatReader().decode(bitmap, null);
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        ZxingEncoding zxingEncodingUtils = new ZxingEncoding();
        String imgPath = "C:\\QrCode\\123.png";
        String contents = "12452573434326";
        int width = 105, height = 50;
        zxingEncodingUtils.encode(contents, width, height, imgPath);
        System.out.println("finished zxing ITF encode.");
        String decodeContent = zxingEncodingUtils.decode(imgPath);
        System.out.println("解码内容如下：" + decodeContent);
        System.out.println("finished zxing ITF decode.");
    }

}